package com.xinqi.modules.exam.paper.controller.web;

import com.xinqi.common.base.data.IdsDTO;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.R;
import com.xinqi.modules.exam.paper.dto.req.CourseExamPaperCreateDTO;
import com.xinqi.modules.exam.paper.dto.req.CourseExamPaperQueryDTO;
import com.xinqi.modules.exam.paper.dto.req.CourseExamPaperReleaseDTO;
import com.xinqi.modules.exam.paper.dto.req.CourseExamPaperUpdateDTO;
import com.xinqi.modules.exam.paper.dto.rsp.CourseExamPaperDetailDTO;
import com.xinqi.modules.exam.paper.dto.rsp.CourseExamPaperResultDTO;
import com.xinqi.modules.exam.paper.dto.rsp.CourseExamPaperStatusNumDTO;
import com.xinqi.modules.exam.paper.dto.rsp.CourseExamPaperStudentDTO;
import com.xinqi.modules.exam.paper.service.CourseExamPaperService;
import com.xinqi.modules.exam.student.service.CourseExamPaperStudentService;
import com.xinqi.modules.exam.utils.StudentHelp;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 课程测评WEB控制层
 *
 * @author: linkai
 * @date: 2023/07/02
 */
@Tag(name = "Web-课程测评", description = "课程测评Web接口")
@RestController
@RequestMapping("/web/exam-paper")
@RequiredArgsConstructor
public class CourseExamPaperWebController {
    private final StudentHelp studentHelp;
    private final CourseExamPaperService courseExamPaperService;
    private final CourseExamPaperStudentService courseExamPaperStudentService;

    /**
     * 课程测评-分页-老师端
     */
    @Operation(summary = "课程测评-分页-老师端")
    @GetMapping("/page")
    public R<PageInfo<CourseExamPaperResultDTO>> page(@ParameterObject CourseExamPaperQueryDTO dto) {
        CommonErrors.BAD_REQUEST.check(dto.getClassId() != null, "班级ID不能为空");
        PageInfo<CourseExamPaperResultDTO> page = courseExamPaperService.page(dto, dto);
        return R.data(page);
    }

    /**
     * 课程测评-列表-老师端
     */
    @Operation(summary = "课程测评-列表-老师端")
    @GetMapping("/list")
    public R<List<CourseExamPaperResultDTO>> list(@ParameterObject CourseExamPaperQueryDTO dto) {
        CommonErrors.BAD_REQUEST.check(dto.getClassId() != null, "班级ID不能为空");
        List<CourseExamPaperResultDTO> list = courseExamPaperService.findList(dto);
        return R.data(list);
    }

    /**
     * 课程测评-列表-学生端
     */
    @Operation(summary = "课程测评-列表-学生端")
    @GetMapping("/student-list")
    public R<List<CourseExamPaperStudentDTO>> studentList(@ParameterObject CourseExamPaperQueryDTO dto) {
        CommonErrors.BAD_REQUEST.check(dto.getClassId() != null, "班级ID不能为空");
        // 设置学生ID
        dto.setStudentId(studentHelp.getStudentId(dto.getClassId()));
        List<CourseExamPaperStudentDTO> list = courseExamPaperService.findStudentList(dto);
        return R.data(list);
    }

    /**
     * 课程测评-分页-学生端
     */
    @Operation(summary = "课程测评-分页-学生端")
    @GetMapping("/student-page")
    public R<PageInfo<CourseExamPaperStudentDTO>> studentPage(@ParameterObject CourseExamPaperQueryDTO dto) {
        CommonErrors.BAD_REQUEST.check(dto.getClassId() != null, "班级ID不能为空");
        // 设置学生ID
        dto.setStudentId(studentHelp.getStudentId(dto.getClassId()));
        PageInfo<CourseExamPaperStudentDTO> page = courseExamPaperService.pageStudentList(dto, dto);
        return R.data(page);
    }

    /**
     * 课程测评-详情
     */
    @Operation(summary = "课程测评-详情")
    @GetMapping("/{id}")
    public R<CourseExamPaperDetailDTO> findById(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id) {
        CourseExamPaperDetailDTO dto = courseExamPaperService.findDetailById(id);
        return R.data(dto);
    }

    /**
     * 新增课程测评
     */
    @Operation(summary = "新增课程测评")
    @PostMapping
    public R<Long> insert(@RequestBody CourseExamPaperCreateDTO dto) {
        return R.data(courseExamPaperService.insert(dto));
    }

    /**
     * 修改课程测评
     */
    @Operation(summary = "修改课程测评")
    @PutMapping("/{id}")
    public R<Boolean> update(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id,
                             @RequestBody CourseExamPaperUpdateDTO dto) {
        dto.setId(id);
        return R.data(courseExamPaperService.updateById(dto));
    }

    /**
     * 发布测评
     */
    @Operation(summary = "发布测评")
    @PutMapping("/{id}/release")
    public R<Boolean> release(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id,
                              @RequestBody CourseExamPaperReleaseDTO dto) {
        dto.setExamPaperId(id);
        return R.data(courseExamPaperService.release(dto));
    }

    /**
     * 删除课程测评
     */
    @Operation(summary = "删除课程测评")
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id) {
        return R.data(courseExamPaperService.deleteById(id));
    }

    /**
     * 批量删除 课程测评
     */
    @Operation(summary = "批量删除 课程测评")
    @PostMapping("/bach-delete")
    public R<Boolean> bachDelete(@RequestBody IdsDTO ids) {
        return R.data(courseExamPaperService.deleteByIds(ids.getIds()));
    }

    /**
     * 手动结束测评
     */
    @Operation(summary = "手动结束测评")
    @DeleteMapping("/{id}/manual-end")
    public R<Boolean> manualEnd(@Parameter(description = "id", example = "100001") @PathVariable("id") Long id) {
        return R.data(courseExamPaperService.manualEnd(id));
    }

    /**
     * 测评状态数量统计
     */
    @Operation(summary = "测评状态数量统计-老师端")
    @GetMapping("/status-num-stats/{class-id}")
    public R<CourseExamPaperStatusNumDTO> findStatusNum(
        @Parameter(description = "班级ID", example = "100001") @PathVariable("class-id") Long classId
    ) {
        return R.data(courseExamPaperService.findStatusNum(classId));
    }

    /**
     * 测评状态数量统计
     */
    @Operation(summary = "测评状态数量统计-学生端")
    @GetMapping("/status-num-stats/{class-id}/student")
    public R<CourseExamPaperStatusNumDTO> findStudentStatusNum(
        @Parameter(description = "班级ID", example = "100001") @PathVariable("class-id") Long classId
    ) {
        Long studentId = studentHelp.getStudentId(classId);
        return R.data(courseExamPaperService.findStudentStatusNum(classId, studentId));
    }

    /**
     * 更新测评统计数据
     */
    @Operation(summary = "更新测评统计数据")
    @PutMapping("/stats-data/{exam-id}")
    public R<Boolean> updateStatusData(
        @Parameter(description = "测评ID", example = "100001") @PathVariable("exam-id") Long examId
    ) {
        courseExamPaperStudentService.updateStatsData(examId);
        return R.data(true);
    }

}
